package com.uenpay.im.panel.emojitype;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.uenpay.im.R;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-4-8 下午5:41:37
 * @copyRight http://www.qiyunxin.com
 * @function 表情面板底部按钮适配器
 */
public class BottomEmojiAdapter extends
		RecyclerView.Adapter<BottomEmojiAdapter.BottomEmojiViewHodler> {

	private ArrayList<CheckEmojiEntity> arrayList = null;
	private Context mContext;
	private IOnItemClick mIOnItemClick;

	public BottomEmojiAdapter(Context _Context,
			ArrayList<CheckEmojiEntity> _ArrayList, IOnItemClick _IOnItemClick) {
		arrayList = _ArrayList;
		mContext = _Context;
		mIOnItemClick = _IOnItemClick;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void setSelectedItem(int id) {
		if (arrayList != null) {
			for (int i = 0, size = arrayList.size(); i < size; i++) {
				if (arrayList.get(i).id == id) {
					arrayList.get(i).isSelected = true;
					;
				} else {
					arrayList.get(i).isSelected = false;
				}
			}
			notifyDataSetChanged();
		}
	}

	static class BottomEmojiViewHodler extends RecyclerView.ViewHolder {
		public BottomEmojiViewHodler(View arg0) {
			super(arg0);
			imageView = (ImageView) arg0.findViewById(R.id.imageview);
			bg_layout = (RelativeLayout) arg0.findViewById(R.id.bg_layout);
		}

		private ImageView imageView;
		private RelativeLayout bg_layout;
	}

	@Override
	public int getItemCount() {
		return arrayList == null ? 0 : arrayList.size();
	}

	@Override
	public void onBindViewHolder(BottomEmojiViewHodler viewHodler,
			final int position) {
		// TODO 设置值
		if (arrayList.get(position).id == -1
				&& arrayList.get(position).resourceId == -1
				&& TextUtils.isEmpty(arrayList.get(position).resourceName)) {
			viewHodler.imageView.setImageResource(R.drawable.kwe);
		} else {
			viewHodler.imageView
					.setImageResource(arrayList.get(position).resourceId);
		}
		if (arrayList.get(position).isSelected) {
			viewHodler.bg_layout.setBackgroundColor(mContext.getResources()
					.getColor(R.color.the_color_pale_gray));
		} else {
			viewHodler.bg_layout.setBackgroundColor(mContext.getResources()
					.getColor(R.color.the_color_white));
		}
		viewHodler.bg_layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (mIOnItemClick != null && arrayList != null
						&& arrayList.size() > 0) {
					mIOnItemClick.onItemClick(arrayList.get(position));
				}
			}
		});
	}

	@Override
	public BottomEmojiViewHodler onCreateViewHolder(ViewGroup viewGroup,
			int arg1) {
		View view = View.inflate(viewGroup.getContext(),
				R.layout.item_bottom_emoji_layout, null);
		// 创建一个ViewHolder
		BottomEmojiViewHodler holder = new BottomEmojiViewHodler(view);
		return holder;
	}

	public interface IOnItemClick {
		public void onItemClick(CheckEmojiEntity mCheckEmojiEntity);
	}

}
